<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">考勤报表</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>考勤报表</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            <!--            添加理由：由于具有查看所有人的权限所以添加可以选择的签到-->
            select选择框：
            <select id="sign3-select">
                <option value="user_id" selected="selected">-个人报表-</option>
                <option value="all">-成员报表-</option>
                <!--                <option value="username">账号</option>-->
                <!--                <option value="nick_name">用户名</option>-->
                <!--                <option value="phone">手机号</option>-->
                <!--                <option value="email">邮箱</option>-->
            </select>&emsp;
            <!--            根据签到的状态选择搜索的对象-->


            日期：
            <input id="timeStart" class="layui-input search-input" type="date" placeholder="输入：YYYY:MM:DD"/>至
            <input id="timeEnd" class="layui-input search-input" type="date" placeholder="输入：YYYY:MM:DD"/>
            <!--            根据时间查询签到表中时间段：如查询9:00-9:30的签到记录：包括签到和签退-->

            <!--            根据昵称查询签到表中的数据-->
            用户昵称：
            <input id="nickName" class="layui-input search-input" type="text" placeholder="输入用户昵称"/>&emsp;
            <button id="sign-btn-sreach" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>搜索</button>
            <button id="sign-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
        </div>

        <table class="layui-table" id="signCount-table" lay-filter="signCount-table"></table>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'util', 'config', 'index', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var index = layui.index;
        var admin = layui.admin;
        form.render();

        // 添加select绑定权限
        if(!admin.hasPerm('sign:post/sign/userSelect')) {
            $('#sign3-select').append(" <option value=\"all\">-成员报表-</option>");
        }
        // 考勤时间管理按钮绑定权限
        // if(!admin.hasPerm('sign:post/sign/findSignInAndSignOutTime')) {
        //     $('#attendance-btn-time').hide();
        // }
        var login_user = '';
        // 获取当前用户信息
        index.getUser(function (user) {
            login_user = user;
            console.log(login_user)
        });

        var userSelect=$("#sign3-select").val();
        var userId;
        if(userSelect!="all"){
            userId=login_user.id;
        }else{
            userId="";
        }
        var timeStart=getNowFormatDate();
        $("#timeStart").val(timeStart);
        var timeEnd=getNowFormatDate();
        $("#timeEnd").val(timeEnd);
        timeStart=$("#timeStart").val();
        timeEnd=$("#timeEnd").val();

            function getNowFormatDate() {
                var date = new Date();
                var seperator1 = "-";
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var strDate = date.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                var currentdate = year + seperator1 + month + seperator1 + strDate;
                return currentdate;
            }
        // 渲染表格
        table.render({
            elem: '#signCount-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-sign/sign/signCount',
            method: 'POST',
            where: {
                access_token: config.getToken().access_token,
                userId:userId,
                timeStart:timeStart,
                timeEnd:timeEnd
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                //修改理由：由于不用展示userId字段故注释
                // {field: 'userId', sort: true, title: '用户编号'},
                //添加理由：在签到栏一项需要展示用户昵称字段；在model层增加nickName字段
                {field: 'nickName', sort: true, title: '用户昵称'},
                {field: 'signinCount', sort: true, title: '签到次数'},
                {field: 'istate1Count', sort: true, title: '已到次数'},
                {field: 'istate2Count', sort: true, title: '迟到次数'},
                {field: 'sigoutCount', sort: true, title: '签退次数'},
                {field: 'ostate1Count', sort: true, title: '已退次数'},
                {field: 'ostate2Count', sort: true, title: '早退次数'},
                {field: 'unsigninCount', sort: true, title: '未签到次数'},
                {field: 'unsigoutCount', sort: true, title: '未签退次数'}
            ]]
        });
        // 搜索按钮点击事件
        $('#sign-btn-sreach').click(function () {
            //查询用户昵称：nickName
            var nickName = $('#nickName').val();
            //     // //查询开始日期：timeStart
            var timeStart = $('#timeStart').val();
            //     // //查询结束日期：timeEnd
            var timeEnd=$('#timeEnd').val();

            var userSelect=$("#sign3-select").val();
            var userId;
            if(userSelect!="all"){
                userId=login_user.id;
            }else{
                userId="";
            }
            let data={
                nickName:nickName,
                userId:userId,
                timeStart:timeStart,
                timeEnd:timeEnd
            }
            //     // table.reload('sign-table', {where: {nickName: nickName,timeStart: timeStart,timeEnd:timeEnd,dateStart:dateStart,dateEnd:dateEnd,istate:istate,ostate:ostate}});
            //     table.reload('sign-table',{where:{nickName:nickName,userId: login_user.id}});
            table.render({
                elem: '#signCount-table',
                // url: config.base_server + 'user_query.json',
                url: config.base_server + 'api-sign/sign/signCount',
                method: 'POST',
                where: {
                    access_token: config.getToken().access_token,
                    nickName:nickName,
                    userId:userId,
                    timeStart:timeStart,
                    timeEnd:timeEnd
                },
                page: true,
                cols: [[
                    {type: 'numbers'},
                    //修改理由：由于不用展示userId字段故注释
                    // {field: 'userId', sort: true, title: '用户编号'},
                    //添加理由：在签到栏一项需要展示用户昵称字段；在model层增加nickName字段
                    {field: 'nickName', sort: true, title: '用户昵称'},
                    {field: 'signinCount', sort: true, title: '签到次数'},
                    {field: 'istate1Count', sort: true, title: '已到次数'},
                    {field: 'istate2Count', sort: true, title: '迟到次数'},
                    {field: 'sigoutCount', sort: true, title: '签退次数'},
                    {field: 'ostate1Count', sort: true, title: '已退次数'},
                    {field: 'ostate2Count', sort: true, title: '早退次数'},
                    {field: 'unsigninCount', sort: true, title: '未签到次数'},
                    {field: 'unsigoutCount', sort: true, title: '未签退次数'}
                    // {field: 'time', sort: true, title: '签到日期'},
                    // {align: 'center', toolbar: '#sign-table-bar', title: '操作', width: 150}
                ]]
            });
        });

        // //工具条点击事件
        // table.on('tool(sign-table)', function (obj) {
        //     var data = obj.data;
        //     var layEvent = obj.event;
        //
        //     if (layEvent === 'edit') {// 修改
        //         showEditModel(data);
        //     }else if(layEvent==="delete"){//删除
        //         deleteSign(data);
        //     }
        // });

        // //删除当前数据
        // var deleteSign=function (data) {
        //     layer.confirm('是否确定删除？', function (i) {
        //         layer.close(i);
        //         layer.load(2);
        //         admin.req('api-sign/sign/signDelete',JSON.stringify(data), function (data) {
        //             layer.closeAll('loading');
        //             if (data.code == 0) {
        //                 layer.msg(data.msg, {icon: 1, time: 500});
        //             } else {
        //                 layer.msg(data.msg, {icon: 2, time: 500});
        //             }
        //         }, 'POST');
        //         table.reload('sign-table', {});
        //     });
        // }

        // //显示表单弹窗
        // var showEditModel = function (data) {
        //     //修改sex值
        //     if (data) {
        //         data.state = data.state + '';
        //     }
        //     admin.putTempData('t_sign', data);
        //     var title =  '签到修改' ;
        //     admin.popupCenter({
        //         title: title,
        //         path: 'pages/attendance/signUpdate.html',
        //         area:['1200px','800px'],
        //         finish: function () {
        //             table.reload('sign-table', {});
        //         }
        //     });
        // };

        // 导出
        $('#sign-btn-export').click(function () {
            // let data={
            //     access_token: config.getToken().access_token,
            //     userId: login_user.id
            // }
            var url = config.base_server + 'api-sign/sign/exportKaoqin?access_token='+config.getToken().access_token+'&userId='+login_user.id;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.responseType = "blob";
            xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            xhr.onload = function () {
                if (this.status == 200) {
                    var blob = this.response;
                    var objecturl = URL.createObjectURL(blob);
                    window.location.href = objecturl;
                }
            }
            xhr.send();
        });



        // //select select的更改触发
        // $("#sign-select").change(function () {
        //     console.log($(this).val());
        //     var options=$(this).val();
        //     table.reload('sign-table',{where:{options:options}});
        // })
    });
</script>